package ModelLayer;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Set;

/**
 * This class is containing a HashMap of Loan objects. Including methods for adding
 * loan objects, returning loan objects individually and as an ArrayList.
 * 
 * @author (Alexander Nikolaj Bach Pedersen) 
 * @version (17-04-2013)
 */
public class LoanContainer
{
    // instance variables - replace the example below with your own
    private static LoanContainer instance;
    private static int index;
    private HashMap<Integer,Loan> loanContainer;
    

    /**
     * Constructor for objects of class LoanContainer
     */
    public LoanContainer()
    {
        // initialise instance variables
        loanContainer = new HashMap<Integer,Loan>();
        index = 0;
    }

    /**
     * Creating a new instance of the class LoanContainer if there is 
     * no instance of it
     */
    public static LoanContainer getInstance()
    {
        if(instance == null)
        {
            instance = new LoanContainer();
        }
        return instance;
    }

    /**
     * Will add a Loan object to the loanContainer. With each new created loan
     * the index will be incremented by 1 and the ID will be equal to the index, 
     * so that each Loan will get an unique ID.
     * 
     * @param loan      an object of Loan.
     * @return          the newly created Loan.
     */
    public Loan addLoan(Loan loan)
    {
        index++;
        loan.setId(index);
        loanContainer.put(loan.getId(), loan);
        return loan;
    }
    
    /**
     * Returns a Loan object by entering the id of the desired Loan.
     * 
     * @param id    the id of a Loan object.
     * @return      the Loan object with the entered id.
     */
    public Loan getLoan(int id)
    {
        return loanContainer.get(id);
    }
    
    /**
     * Returns an ArrayList containing all Loan objects which is stored in the 
     * loanContainer HashMap. The objects stored in the HashMap is added individually
     * to the loans ArrayList.
     * 
     * @return      an ArrayList of all loans.
     * 
     * Rename method to listLoans.
     */
    public ArrayList<Loan> listActiveLoans()
    {
        ArrayList<Loan> loans = new ArrayList<Loan>();
        Set<Integer> indexes = loanContainer.keySet();
        for(Integer index : indexes)
        {
            Loan loan = loanContainer.get(index);
            loans.add(loan);
        }
        return loans;
    }
}
